package com.gree.scmservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.enums.BusinessType;
import com.gree.common.utils.StringUtils;
import com.gree.framework.entity.SysUser;
import com.gree.scmservice.dao.LogInfoDao;
import com.gree.scmservice.feign.AuthServiceApiFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.nutz.dao.entity.Record;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 在线用户
 * Created by 180686 on 2020/11/17 14:48
 */

@RestController
@Api(tags = "系统信息:登录日志接口")
@RequestMapping("/monitor")
public class OnlineUserController {

    @Autowired
    private AuthServiceApiFeign authServiceApiFeign;

    @Autowired
    private LogInfoDao logInfoDao;

    /**
     * 获取在线用户数量
     * @return
     */
    @GetMapping("/online/count")
    @ApiOperation(value = "获取在线用户数量")
    public Object getOnlineCount(){
        Object object = authServiceApiFeign.getOnlineCount();
        Integer count = (Integer) object;
        return new ReturnVo("200","success",count) ;
    }

    /**
     * 分页获取在线用户
     * @param map
     * @return
     */
    @ApiOperation(value = "分页获取在线用户")
    @ApiImplicitParam(name = "map", value = "用户登录日志", dataType = "HashMap")
    @PostMapping("/online/list")
    public Object list(@RequestBody HashMap<String,Object> map){
        Object object = authServiceApiFeign.getOnlineUser();
        ArrayList arrayList = (ArrayList) object;
        JSONArray jsonArray= JSONArray.parseArray(JSON.toJSONString(arrayList));
        ArrayList<HashMap> datalist = new ArrayList<>();
        int total = jsonArray.size();
        for(Object object2:jsonArray){
            NutMap nutMap = NutMap.NEW();
            JSONObject jsonObject = JSONObject.parseObject(object2.toString());
            SysUser sysUser = JSON.toJavaObject(jsonObject.getJSONObject("sysUser"),SysUser.class);
            Record record = logInfoDao.getLoginLogByNameLatest(sysUser.getUsid());
            nutMap.addv("tokenId",jsonObject.getString("token"));
            nutMap.addv("deptName",sysUser.getDscc());
            nutMap.addv("compName",sysUser.getDscd());
            nutMap.addv("userName",sysUser.getNama());
            nutMap.addv("userId",sysUser.getUsid());
            if(map.get("userId")!=null){
                if(!StringUtils.isEmpty(map.get("userId").toString()) && !sysUser.getUsid().equals(map.get("userId").toString())){
                    continue;
                }
            }
            if(record!=null){
                if(map.get("ipaddr")!=null){
                    if(!StringUtils.isEmpty(map.get("ipaddr").toString()) && !record.getString("ipaddr").split(",")[0].equals(map.get("ipaddr").toString())){
                        continue;
                    }
                }
                nutMap.addv("ipaddr",record.getString("ipaddr").split(",")[0]);
                nutMap.addv("loginLocation",record.getTimestamp("login_location"));
                nutMap.addv("browser",record.getString("browser"));
                nutMap.addv("os",record.getString("os"));
                nutMap.addv("loginTime",record.getTimestamp("login_time"));
                nutMap.addv("ontime",(System.currentTimeMillis() - record.getTimestamp("login_time").getTime()) / (1000 * 60));
            }
            datalist.add(nutMap);
        }
        return new ReturnVo("200","success",NutMap.NEW().addv("datalist",datalist).addv("total",total)) ;
    }

    /**
     * 强退在线用户
     * @param tokenId
     * @return
     */
    @Log(title = "在线用户模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/online/{tokenId}")
    @ApiOperation(value = "强退在线用户")
    @ApiImplicitParam(name = "tokenId", value = "用户token ID", dataType = "String")
    public Object forceLogout(@PathVariable String tokenId){
        Object object = authServiceApiFeign.forceLogout(tokenId);
        return object;
    }
}
